﻿Imports DTO
Imports System.Data.SqlClient
Imports System.Data.Sql
Imports System.Data
Public Class PhieuNhapDAO

    Public Sub Them(ByVal pndto As PhieuNhapDTO)
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        Dim cn As SqlConnection
        cn = DatabaseProvider.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim strSQL As String
        strSQL = "INSERT INTO PHIEUNHAP(NgayNhap) values(@NgayNhap)"

        'B4: Thuc thi chuoi strSQL
        Dim cmd As New SqlCommand(strSQL, cn)

        cmd.Parameters.Add("@NgayNhap", SqlDbType.DateTime)
        cmd.Parameters("@NgayNhap").Value = pndto.NgayNhap

        cmd.ExecuteNonQuery()

        strSQL = "Select @@IDENTITY"
        cmd = New SqlCommand(strSQL, cn)
        pndto.MaPhieuNhap = cmd.ExecuteScalar()
        
        'B5: Dong ket noi CSDL
        cn.Close()
    End Sub

    Public Function LayBang() As DataTable
        Dim dt As New DataTable()
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        Dim cn As SqlConnection
        cn = DatabaseProvider.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim strSQL As String
        strSQL = "Select * From PHIEUNHAP"
        'B4: Thuc thi chuoi strSQL
        Dim da As New SqlDataAdapter(strSQL, cn)
        da.Fill(dt)
        'B5: Dong ket noi CSDL
        cn.Close()
        Return dt
    End Function

End Class
